﻿package com.badibadi.activity;

import com.badibadi.adapter.RegionHomePageAdapter;
import com.badibadi.infos.Area_Main_display_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.TempTools;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 地区主页final
 * 
 * @author Administrator region_home_page_background
 * 
 */
public class RegionHomePageActivity extends BaseActivity {

	private ExpandableListView elv;
	private MySharePreferences preferences;
	private int languageType = 0;
	private RegionHomePageAdapter rhp_adapter;
	// private ImageView region_home_page_background;
	private Results results;
	private TextView region_home_page_titalname;
	private Area_Main_display_Model display_Model;
	private DisplayImageOptions options;
	private String areaid;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(RegionHomePageActivity.this);
				try {
					Utils.showMessage(RegionHomePageActivity.this,
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
				Utils.ExitPrgress(RegionHomePageActivity.this);
				try {
					init();
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				Utils.ExitPrgress(RegionHomePageActivity.this);
				try {
					Utils.showMessage(RegionHomePageActivity.this,
							getResources().getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 4:
				Utils.ExitPrgress(RegionHomePageActivity.this);
				try {
					Utils.showMessage(RegionHomePageActivity.this,
							results.getRetmsg());
				} catch (Exception e) {
					   
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.region_home_page);
		try {
			areaid = getIntent().getStringExtra("area_id");
			allArea(areaid);
		} catch (Exception e) {
			// TODO: handle exception
			allArea("117");
		}
		// init();
	}

	private void init() {
		// TODO Auto-generated method stub
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
		preferences = new MySharePreferences(RegionHomePageActivity.this,
				"language");
		languageType = preferences.get("language", 0);
		elv = (ExpandableListView) this
				.findViewById(R.id.region_home_page_expandableListView1);
		elv.setGroupIndicator(null); // 去掉父类的箭头
		// region_home_page_background = (ImageView)
		// findViewById(R.id.region_home_page_background);// 地区背景图片
		region_home_page_titalname = (TextView) findViewById(R.id.region_home_page_titalname);

		if (display_Model.getArea() != null)
			region_home_page_titalname.setText(""
					+ display_Model.getArea().getArea_name());
		// try {
		// ImageLoader.getInstance().displayImage(
		// Constants.BadiDownImgUrl
		// + display_Model.getArea().getBackground(),
		// region_home_page_background, options);
		// } catch (Exception e) {
		//    
		// }
		this.findViewById(R.id.region_home_page_before_index)
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						startActivity(new Intent(RegionHomePageActivity.this,
								IndexActivity.class));
					}
				});
		this.findViewById(R.id.region_home_page_back).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						RegionHomePageActivity.this.finish();
					}
				});

		rhp_adapter = new RegionHomePageAdapter(RegionHomePageActivity.this,
				display_Model);

		elv.setAdapter(rhp_adapter);
		// 展开所有
		for (int i = 0, length = rhp_adapter.getGroupCount(); i < length; i++) {
			elv.expandGroup(i);
		}
		elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub
				return true;// 默认为false，设为true时，点击事件不会展开Group
			}
		});

	}

	private void allArea(final String areaid) {
		Utils.showPrgress(RegionHomePageActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {

				String content = Utils.getRequest(Constants.BadiUrl_1
						+ "area/allArea?areaId=" + areaid + "&languageType="
						+ TempTools.BackLanguage(languageType));
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   ("地区首页数据显示是:" + content);
//						   
						display_Model = new Area_Main_display_Model();
						try {
							display_Model = JSONUtils.getEntityByJsonString(
									results.getRetmsg(),
									Area_Main_display_Model.class);
							System.out.println("diquzhuye"+results.getRetmsg());
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}
}
